package cn.loioi.multi.service.impl.uesr;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.loioi.multi.mysql.been.SysResourceMapper;
import cn.loioi.multi.mysql.been.SysRoleMapper;
import cn.loioi.multi.mysql.been.SysUserMapper;
import cn.loioi.multi.mysql.model.SysResource;
import cn.loioi.multi.mysql.model.SysRole;
import cn.loioi.multi.mysql.model.SysUser;
import cn.loioi.multi.service.user.LoginService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class LoginServiceImpl implements LoginService {
	private @Resource SysUserMapper userMapper;
	private @Resource SysRoleMapper roleMapper;
	private @Resource SysResourceMapper resourceMapper;

	@Override
	public String login(String account) {
		if (StringUtils.isEmpty(account)) return null;
		return userMapper.login(account);
	}

	@Override
	public List<String> roles(String account) {
		if (StringUtils.isEmpty(account)) return null;
		return roleMapper.roles(account);
	}

	@Override
	public List<String> permission(String account) {
		if (StringUtils.isEmpty(account)) return null;
		return resourceMapper.permission(account);
	}

	@Override
	public SysUser getUser(String account) {
		if (StringUtils.isEmpty(account)) return null;
		Example example = new Example(SysUser.class);
		Criteria criteria = example.createCriteria();
		criteria.andCondition("account=", account);
		List<SysUser> list = userMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<SysRole> getRoles(String account) {
		if (StringUtils.isEmpty(account)) return null;
		return roleMapper.getRoles(account);
	}

	@Override
	public List<SysResource> getRsources(String account) {
		if (StringUtils.isEmpty(account)) return null;
		return resourceMapper.resources(account);
	}
}
